home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: lion.cs.latrobe.edu.au!boylesgj
- From: boylesgj@lion.cs.latrobe.edu.au (Gregary J Boyles)
- Subject: C++ problem with constructor.
- X-Nntp-Posting-Host: lion.cs.latrobe.edu.au
- Message-ID: <DoGGGp.Muy@latcs1.lat.oz.au>
- Sender: news@latcs1.lat.oz.au (news)
- Organization: Comp.Sci & Comp.Eng, La Trobe Uni, Australia
- Date: Mon, 18 Mar 1996 08:48:25 GMT
-
- See astericks
-
- // main.cpp
-
- #include <iostream.h>
- #include "defines.h"
- #include "address.h"
-
- int main()
- {
- Address Address1(56,"Derby Drive","Epping",3165);
- Address Address2(22,"Claremont Street","Fawkner",3060);
-
- /********************************************************
- After the above two calls Address1 and Address2 contain
- all the appropriate data however after the next call all
- 3 contain 0 or null strings. WHY?
- ********************************************************/
-
- Address Address3(Address1);
- return(0);
- }
-
-
-
- // address.cpp
-
- #include "address.h"
- #include <string.h>
-
- // Constructors
- Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
- {
- Number=ANumber;
- strcpy(Street,AStreet);
- strcpy(City,ACity);
- Zip=AZip;
- }
-
- Address::Address()
- {
- Number=0;
- strcpy(Street,"");
- strcpy(City,"");
- Zip=0;
- }
-
- // Copy constructor
- Address::Address(Address& AnAddress)
- {
- Number=AnAddress.Number;
- strcpy(Street,AnAddress.Street);
- strcpy(City,AnAddress.City);
- Zip=AnAddress.Zip;
- }
-
- // Deconstructor
- Address::~Address()
- {
- Number=0;
- strcpy(Street,"");
- strcpy(City,"");
- Zip=0;
- }
-
-
- // address.h
-
- #ifndef __ADDRESS_H
- #define __ADDRESS_H
- #define STR_STREET " street"
-
- #include "defines.h"
-
- class Address
- {
- private : int Number;
- char *Street;
- char *City;
- int Zip;
-
- public : // Constructors
- Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
- Address();
- // Copy constructor
- Address(Address& AnAddress);
- // Deconstructor
- ~Address();
- };
-
- #endif
-